<?php
class photomodel {
		var $db;
		var $base;
		function photomodel(&$base){
			$this->base = $base;
			$this->db = $base->db;
		}
		function upload($row){
			$result = $this->db->query("INSERT INTO ".AM_DBTABLEPRE."photos VALUES('','{$row['album_id']}','{$row['user_id']}','{$row['title']}','','{$row['created']}','{$row['filename']}','{$row['filesize']}','{$row['type']}','{$row['url']}','{$row['thumb']}','0','0')");
			return $this->db->insert_id();
		}
		function photolist($albumid,$from,$num)
		{
			$result = $this->db->fetch_all("SELECT * FROM ".AM_DBTABLEPRE."photos WHERE album_id={$albumid} ORDER BY photo_id DESC LIMIT {$from},{$num}");
			return $result;
		}
		function photo_all($albumid)
		{
			$result = $this->db->fetch_all("SELECT * FROM ".AM_DBTABLEPRE."photos WHERE album_id={$albumid}");
			return $result;			
		}
		function photoview($photoid)
		{
			$result = $this->db->fetch_first("SELECT * FROM ".AM_DBTABLEPRE."photos WHERE photo_id = {$photoid} LIMIT 1 ");
			return $result;
		}
		function get_albumid($photoid)
		{
			$result = $this->db->fetch_first("SELECT album_id FROM ".AM_DBTABLEPRE."photos WHERE photo_id = {$photoid} LIMIT 1 ");
			return $result;
		}
		function get_uid($photoid)
		{
			$result = $this->db->fetch_first("SELECT user_id FROM ".AM_DBTABLEPRE."photos WHERE photo_id = {$photoid} LIMIT 1 ");
			return $result['user_id'];
		}		
		function get_last($photoid,$album_id)
		{
			$result = $this->db->fetch_first("SELECT * FROM ".AM_DBTABLEPRE."photos WHERE photo_id<{$photoid} AND album_id={$album_id} ORDER BY photo_id DESC LIMIT 1");
			if(!$result)
			{
				$photodata = $this->db->fetch_first("SELECT * FROM ".AM_DBTABLEPRE."photos WHERE album_id={$album_id} ORDER BY photo_id DESC LIMIT 1");
				return $photodata;
			}
			else
			return $result;
		}
		function get_next($photoid,$album_id)
		{
			$result = $this->db->fetch_first("SELECT * FROM ".AM_DBTABLEPRE."photos WHERE photo_id>{$photoid} AND album_id={$album_id} ORDER BY photo_id ASC LIMIT 1");
			if(!$result)
			{
				$photodata = $this->db->fetch_first("SELECT * FROM ".AM_DBTABLEPRE."photos WHERE album_id={$album_id} ORDER BY photo_id ASC LIMIT 1");
				return $photodata;
			}
			else
			return $result;
		}			
		function update($row,$photoid)
		{
			$sql = 'UPDATE '.AM_DBTABLEPRE.'photos SET ';
			foreach($row as $key=>$value)
			{
				$sql.="$key".' = '."$value".',';
			}
			$sql = substr($sql,0,-1)." WHERE photo_id={$photoid}";
			$result = $this->db->query($sql);
			return $result;
		}
		function get_count_by_albumid($aid)
		{
			$result = $this->db->fetch_first("SELECT COUNT(*) FROM ".AM_DBTABLEPRE."photos WHERE album_id={$aid}");
			return $result['COUNT(*)'];
		}
		function delete_by_albumid($albumid,$uid)
		{
			$result = $this->db->query("DELETE FROM ".AM_DBTABLEPRE."photos WHERE album_id={$albumid} AND user_id={$uid}");
			return $this->db->affected_rows();
		}		
		function delete($photoid,$uid)
		{
			$result = $this->db->query("DELETE FROM ".AM_DBTABLEPRE."photos WHERE photo_id={$photoid} AND user_id={$uid}");
			return $this->db->affected_rows();
		}
		function get_hot($from,$num)
		{
			$result = $this->db->fetch_all("SElECT * FROM ".AM_DBTABLEPRE."photos ORDER BY pviewnum AND pcomment_num DESC LIMIT {$from},{$num}");
			return $result;
		}
		function get_count()
		{
			$result = $this->db->fetch_first("SElECT COUNT(*) FROM ".AM_DBTABLEPRE."photos");
			return $result['COUNT(*)'];
		}		
	}
	?>